package leetcode.editor.cn.q1xx

class Q190 {
    val M1 = 0x55555555 // 01010101010101010101010101010101
    val M2 = 0x33333333 // 00110011001100110011001100110011
    val M3 = 0x0f0f0f0f // 00001111000011110000111100001111
    val M4 = 0x00ff00ff // 00000000111111110000000011111111

    fun reverseBits(n:Int):Int {
        var result = (n ushr 1 and M1) or ((n and M1) shl 1)
        result = (result ushr 2 and M2) or ((result and M2) shl 2)
        result = (result ushr 4 and M3) or ((result and M3) shl 4)
        result = (result ushr 8 and M4) or ((result and M4) shl 8)
        return (result ushr 16) or (result shl 16)
    }
}